package com.hx.controller;

import com.hx.service.ITrainingBasesService;
import com.hx.service.ITrainingEquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/")
public class ImportDataController {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    @Resource
    private TaskExecutor taskExecutor;
    @Autowired
    private ITrainingBasesService trainingBasesService;
    @Autowired
    private ITrainingEquipmentService equipmentService;

    @PostMapping("/import")
    @ResponseBody
    public Map<String, String> importData(@RequestParam("file") MultipartFile file) {
        trainingBasesService.readExcel(file);
        return new HashMap<String, String>() {{
            put("code", "1");
            put("msg", "文件已上传，请等待入库！");
        }};
        //return ResponseEntity.ok("导入请求已接受");
    }

    @PostMapping("/importOfEquipment")
    @ResponseBody
    public Map<String, String> importOfEquipment(@RequestParam("file") MultipartFile file) {
        equipmentService.readExcel(file);
        return new HashMap<String, String>() {{
            put("code", "1");
            put("msg", "文件已上传，请等待入库！");
        }};
        //return ResponseEntity.ok("导入请求已接受");
    }
    /**
     * 处理业务，业务校验、存储
     *
     * @param dataList
     */
    /*private void processData(List<ExcelFieldData> dataList) {
        CompletableFuture.supplyAsync(() -> {
            Integer startId = 0;// data.stream().findFirst().get().getBaseId();
            Integer endId = 0;// data.get((data.size()-1)).getBaseId();
            log.info("{}至{}开始了", startId, endId);
            try {
                List<TrainingBases> collect = dataList.stream().map(item -> BeanUtil.toBean(item, TrainingBases.class)).collect(Collectors.toList());
                trainingBasesService.saveBatch(collect);
                collect.stream().forEach(it -> log.info("id={}", it.getBaseId()));
                simpMessagingTemplate.convertAndSend("/topic/ws", startId + "至" + endId + "入库成功了");
            } catch (DataAccessException e) {
                log.error("批次插入失败，失败数量：{}", dataList.size(), e);
            }
            //清空缓存列表（无论成功与否）
            dataList.clear();
            return startId + "至" + endId + "入库成功了";
        }, taskExecutor).thenAccept(info -> log.info("{}", info));
    }

    @GetMapping("/sendMessage")
    @ResponseBody
    public String sendMessage() {
        simpMessagingTemplate.convertAndSend("/topic/ws", "发送消息XXXX");
        return "消息发送成功";
    }*/
}
